//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.SouthernWall_404.Age_Capitalism.Client.Gui.Screen;

import com.SouthernWall_404.Age_Capitalism.AgeCapitalism;
import com.SouthernWall_404.Age_Capitalism.common.API.Info.ClientStoreInfo;
import com.SouthernWall_404.Age_Capitalism.common.Gui.Menu.ModMerchantMenu;
import com.SouthernWall_404.Age_Capitalism.common.Gui.Menu.ModSettingMerchantMenu;
import com.SouthernWall_404.Age_Capitalism.common.Gui.Menu.ModSettingMerchantMenu;
import com.SouthernWall_404.Age_Capitalism.common.Merchant.StoreClientMerchant;
import com.SouthernWall_404.Age_Capitalism.common.Network.C2S.MerchantClientPacket;
import com.SouthernWall_404.Age_Capitalism.common.Network.ModChannels;
import com.SouthernWall_404.Age_Capitalism.common.World.Entity.StoreBlockEntity;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font.DisplayMode;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.Container;
import net.minecraft.world.entity.npc.ClientSideMerchant;
import net.minecraft.world.entity.npc.VillagerData;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.trading.Merchant;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.item.trading.MerchantOffers;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import java.awt.*;

@OnlyIn(Dist.CLIENT)
public class ModSettingMerchantScreen extends AbstractContainerScreen<ModSettingMerchantMenu> {
    private static final ResourceLocation VILLAGER_LOCATION = new ResourceLocation("textures/gui/container/villager2.png");
    private static final int TEXTURE_WIDTH = 512;
    private static final int TEXTURE_HEIGHT = 256;
    private static final int MERCHANT_MENU_PART_X = 99;
    private static final int PROGRESS_BAR_X = 136;
    private static final int PROGRESS_BAR_Y = 16;
    private static final int SELL_ITEM_1_X = 5;
    private static final int SELL_ITEM_2_X = 35;
    private static final int BUY_ITEM_X = 68;
    private static final int LABEL_Y = 6;
    private static final int NUMBER_OF_OFFER_BUTTONS = 7;
    private static final int TRADE_BUTTON_X = 5;
    private static final int TRADE_BUTTON_HEIGHT = 20;
    private static final int TRADE_BUTTON_WIDTH = 88;
    private static final int SCROLLER_HEIGHT = 27;
    private static final int SCROLLER_WIDTH = 6;
    private static final int SCROLL_BAR_HEIGHT = 139;
    private static final int SCROLL_BAR_TOP_POS_Y = 18;
    private static final int SCROLL_BAR_START_X = 94;
    private static final Component TRADES_LABEL = Component.translatable("merchant.trades");
    private static final Component LEVEL_SEPARATOR = Component.literal(" - ");
    private static final Component DEPRECATED_TOOLTIP = Component.translatable("merchant.deprecated");
    private int shopItem;
    private final TradeOfferButton[] tradeOfferButtons = new TradeOfferButton[7];
    int scrollOff;
    private boolean isDragging;
    private int selected=-1;

    private static final String GUI="gui."+ AgeCapitalism.MODID+".";

    private static final String ADDING_TAG=GUI+"store_block.button.adding_merchant";


    private static final ResourceLocation EDITBUTTON_TEXTURES=new ResourceLocation(AgeCapitalism.MODID,"textures/gui/button/configbutton.png");
    private ImageButton upButton;
    private ImageButton downButton;
    private ImageButton delButton;
    private ImageButton EditButton;
    private ImageButton EditComfirmButton;
    private boolean isEditing=false;
    private boolean isFinished=false;

//    private Button addingButton= Button.builder(Component.literal("+"),
//            b->addMerchant()).build();
    private ImageButton addingButton;

    private MerchantOffers offers=new MerchantOffers();

    public ModSettingMerchantScreen(ModSettingMerchantMenu p_99123_, Inventory p_99124_, Component p_99125_) {
        super(p_99123_, p_99124_, p_99125_);
        this.imageWidth = 276;
        this.inventoryLabelX = 107;

//        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;
//        Merchant merchant= modMerchantMenu.trader;


    }
    private void EditFinished(int index)
    {



        changeMerchant(index);

        refresh();
    }

    private void refresh()
    {
        isEditing=false;
        isFinished=true;
        selected=-1;

        removeWidget(upButton);
        removeWidget(downButton);
        removeWidget(delButton);
        removeWidget(EditButton);
        removeWidget(EditComfirmButton);
    }

    private void EditMode(int index)
    {
        isEditing=true;
//        removeWidget(this.EditButton);

    }

    private void postButtonClick() {
        ((ModSettingMerchantMenu)this.menu).setSelectionHint(this.shopItem);
//        ((ModSettingMerchantMenu)this.menu).tryMoveItems(this.shopItem);
        ModChannels.sendToServer(new MerchantClientPacket(this.shopItem));
    }

    protected void init() {
        super.init();
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        int k = j + 16 + 2;
        addingButton=new ImageButton(15,15,15,15,15,30,15,EDITBUTTON_TEXTURES,64,64,b->addMerchant());

        for(int l = 0; l < 7; ++l) {
            this.tradeOfferButtons[l] = (TradeOfferButton)this.addRenderableWidget(new TradeOfferButton(i + 5, k, l, (p_99174_) -> {
                if (p_99174_ instanceof TradeOfferButton) {
                    this.shopItem = ((TradeOfferButton)p_99174_).getIndex() + this.scrollOff;
                    this.postButtonClick();
                }

            }));
            k += 20;
        }

    }

//    public void renderAddingButton()
//    {
//        int l=20;
//        int h=20;
//        int n1 = (this.width - l) / 20*15;
//        int n2 = (this.height - h) / 20*9;
//
//        addingButton.setHeight(h);
//        addingButton.setWidth(l);
//        addingButton.setPosition(n1,n2);
//
//        addRenderableWidget(addingButton);
//    }
    private void setMerchant(MerchantOffers merchantOffers)
    {
        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;

            modMerchantMenu.setOffers(merchantOffers);
    }

    private void deleteMerchant(int index)
    {
        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;
        Container container=modMerchantMenu.tradeContainer;
        Merchant merchant= modMerchantMenu.trader;

        MerchantOffers merchantOffers=merchant.getOffers();

        // 添加边界检查
        if (index < 0 || index >= merchantOffers.size()) {
            return;
        }
        merchantOffers.remove(index);
        setMerchant(merchantOffers);
        this.shopItem--;

        refresh();
    }
    private void upExchange(int index)
    {
        exchangeMerchant(index,index-1);
    }
    private void downExchange(int index)
    {
        exchangeMerchant(index,index+1);
    }
    private void exchangeMerchant(int index_A,int index_B)
    {

        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;
        Container container=modMerchantMenu.tradeContainer;
        Merchant merchant= modMerchantMenu.trader;

        MerchantOffers merchantOffers=merchant.getOffers();

        if(index_A<0||index_B<0||index_A>=merchantOffers.size()||index_B>=merchantOffers.size())
        {
            return;
        }

        MerchantOffer offerA=merchantOffers.get(index_A);
        MerchantOffer offerB=merchantOffers.get(index_B);

        merchantOffers.set(index_A,offerB);
        merchantOffers.set(index_B,offerA);

        setMerchant(merchantOffers);


        refresh();
    }
    private void changeMerchant(int index)
    {
        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;
        Container container=modMerchantMenu.tradeContainer;
        Merchant merchant= modMerchantMenu.trader;

        MerchantOffers merchantOffers=merchant.getOffers();

        ItemStack A=container.getItem(0).copy();
        ItemStack B=container.getItem(1).copy();
        ItemStack Result=container.getItem(2).copy();
        if(A.isEmpty()||Result.isEmpty())
        {
            return;
        }
        MerchantOffer offer=new MerchantOffer(
                A,
                B,
                Result,
                100000, // 最大使用次数
                5,   // 经验值
                0.15f // 价格乘数
        );

        merchantOffers.set(index,offer);
        setMerchant(merchantOffers);

    }
    private void addMerchant()
    {
        ModSettingMerchantMenu modMerchantMenu = (ModSettingMerchantMenu)this.menu;
        Container container=modMerchantMenu.tradeContainer;
        Merchant merchant= modMerchantMenu.trader;

        MerchantOffers merchantOffers=merchant.getOffers();


//        if(merchant instanceof StoreClientMerchant storeClientMerchant)
//        {
            ItemStack A=container.getItem(0).copy();
            ItemStack B=container.getItem(1).copy();
            ItemStack Result=container.getItem(2).copy();
        if(A.isEmpty()||Result.isEmpty())
        {
            return;
        }
            MerchantOffer offer=new MerchantOffer(
                    A,
                    B,
                    Result,
                    100000, // 最大使用次数
                    5,   // 经验值
                    0.15f // 价格乘数
            );

            merchantOffers.add(offer);
            this.setMerchant(merchantOffers);
//        }
    }


    protected void renderLabels(GuiGraphics p_283337_, int p_282009_, int p_283691_) {
        int i = ((ModSettingMerchantMenu)this.menu).getTraderLevel();
        if (i > 0 && i <= 5 && ((ModSettingMerchantMenu)this.menu).showProgressBar()) {
            Component component = this.title.copy().append(LEVEL_SEPARATOR).append(Component.translatable("merchant.level." + i));
            int j = this.font.width(component);
            int k = 49 + this.imageWidth / 2 - j / 2;
            p_283337_.drawString(this.font, component, k, 6, 4210752, false);
        } else {
            p_283337_.drawString(this.font, this.title, 49 + this.imageWidth / 2 - this.font.width(this.title) / 2, 6, 4210752, false);
        }

        p_283337_.drawString(this.font, this.playerInventoryTitle, this.inventoryLabelX, this.inventoryLabelY, 4210752, false);
        int l = this.font.width(TRADES_LABEL);
        p_283337_.drawString(this.font, TRADES_LABEL, 5 - l / 2 + 48, 6, 4210752, false);
    }

    protected void renderBg(GuiGraphics p_283072_, float p_281275_, int p_282312_, int p_282984_) {
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
                p_283072_.blit(VILLAGER_LOCATION, i, j, 0, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 512, 256);
                MerchantOffers merchantoffers = ((ModSettingMerchantMenu)this.menu).getOffers();
                if (!merchantoffers.isEmpty()) {
                    int k = this.shopItem;
                    if (k < 0 || k >= merchantoffers.size()) {
                        return;
                    }

                    MerchantOffer merchantoffer = (MerchantOffer)merchantoffers.get(k);
                    if (merchantoffer.isOutOfStock()) {
                        p_283072_.blit(VILLAGER_LOCATION, this.leftPos + 83 + 99, this.topPos + 35, 0, 311.0F, 0.0F, 28, 21, 512, 256);
            }
        }

    }

    private void renderProgressBar(GuiGraphics p_281426_, int p_283008_, int p_283085_, MerchantOffer p_282094_) {
        int i = ((ModSettingMerchantMenu)this.menu).getTraderLevel();
        int j = ((ModSettingMerchantMenu)this.menu).getTraderXp();
        if (i < 5) {
            p_281426_.blit(VILLAGER_LOCATION, p_283008_ + 136, p_283085_ + 16, 0, 0.0F, 186.0F, 102, 5, 512, 256);
            int k = VillagerData.getMinXpPerLevel(i);
            if (j >= k && VillagerData.canLevelUp(i)) {
                int l = 100;
                float f = 100.0F / (float)(VillagerData.getMaxXpPerLevel(i) - k);
                int i1 = Math.min(Mth.floor(f * (float)(j - k)), 100);
                p_281426_.blit(VILLAGER_LOCATION, p_283008_ + 136, p_283085_ + 16, 0, 0.0F, 191.0F, i1 + 1, 5, 512, 256);
                int j1 = ((ModSettingMerchantMenu)this.menu).getFutureTraderXp();
                if (j1 > 0) {
                    int k1 = Math.min(Mth.floor((float)j1 * f), 100 - i1);
                    p_281426_.blit(VILLAGER_LOCATION, p_283008_ + 136 + i1 + 1, p_283085_ + 16 + 1, 0, 2.0F, 182.0F, k1, 3, 512, 256);
                }
            }
        }

    }

    private void renderScroller(GuiGraphics p_283030_, int p_283154_, int p_281664_, MerchantOffers p_282877_) {
        int i = p_282877_.size() + 1 - 7;
        if (i > 1) {
            int j = 139 - (27 + (i - 1) * 139 / i);
            int k = 1 + j / i + 139 / i;
            int l = 113;
            int i1 = Math.min(113, this.scrollOff * k);
            if (this.scrollOff == i - 1) {
                i1 = 113;
            }

            p_283030_.blit(VILLAGER_LOCATION, p_283154_ + 94, p_281664_ + 18 + i1, 0, 0.0F, 199.0F, 6, 27, 512, 256);
        } else {
            p_283030_.blit(VILLAGER_LOCATION, p_283154_ + 94, p_281664_ + 18, 0, 6.0F, 199.0F, 6, 27, 512, 256);
        }

    }


    private void renderConfigButton(int start_x,int start_y)
    {
        removeWidget(upButton);
        removeWidget(downButton);
        removeWidget(delButton);
        removeWidget(EditButton);
        removeWidget(EditComfirmButton);
//        Minecraft.getInstance().player.sendSystemMessage(Component.literal(""+EDITBUTTON_TEXTURES));
        EditButton = new ImageButton(start_x,start_y,15,15,0,0,15,EDITBUTTON_TEXTURES,64,64,b->EditMode(this.selected));
        EditComfirmButton = new ImageButton(start_x,start_y,15,15,0,30,15,EDITBUTTON_TEXTURES,64,64,b->EditFinished(this.selected));
        delButton=new ImageButton(start_x+18,start_y,15,15,15,0,15,EDITBUTTON_TEXTURES,64,64,b->deleteMerchant(this.selected));
        upButton=new ImageButton(start_x+65,start_y,15,7,30,0,7,EDITBUTTON_TEXTURES,64,64,b->upExchange(this.selected));
        downButton=new ImageButton(start_x+65,start_y+8,15,7,30,14,7,EDITBUTTON_TEXTURES,64,64,b->downExchange(this.selected));

        if(!isFinished)
        {
            addRenderableWidget(upButton);
            addRenderableWidget(downButton);
            addRenderableWidget(delButton);
            if(isEditing)
            {
                addRenderableWidget(EditComfirmButton);
            }
            else
            {
                addRenderableWidget(EditButton);
            }
        }


    }

    public void render(GuiGraphics p_283487_, int p_281994_, int p_282099_, float p_281815_) {
        this.renderBackground(p_283487_);
        super.render(p_283487_, p_281994_, p_282099_, p_281815_);

        int x = (this.width - this.imageWidth) / 2;
        int y = (this.height - this.imageHeight) / 2;

        this.removeWidget(this.addingButton);
        this.addingButton.setPosition(x+this.imageWidth-20,y+5);
        this.addRenderableWidget(this.addingButton);
        MerchantOffers merchantoffers = ((ModSettingMerchantMenu)this.menu).getOffers();

//        renderAddingButton();

        if (!merchantoffers.isEmpty()) {
            int i = (this.width - this.imageWidth) / 2;
            int j = (this.height - this.imageHeight) / 2;
            int k = j + 16 + 1;
            int l = i + 5 + 5;
            this.renderScroller(p_283487_, i, j, merchantoffers);
            int i1 = 0;

            int renderAmount=0;

            for(int z=0;z<merchantoffers.size();z++) {
                if(z==selected)
                {
                    ++i1;
                    renderConfigButton(l, k + 3);
                    k += 20;
                    continue;
                }


                MerchantOffer merchantoffer=merchantoffers.get(z);
                if (!this.canScroll(merchantoffers.size()) || i1 >= this.scrollOff && i1 < 7 + this.scrollOff) {
                    ItemStack itemstack = merchantoffer.getBaseCostA();
                    ItemStack itemstack1 = merchantoffer.getCostA();
                    ItemStack itemstack2 = merchantoffer.getCostB();
                    ItemStack itemstack3 = merchantoffer.getResult();
                    p_283487_.pose().pushPose();
                    p_283487_.pose().translate(0.0F, 0.0F, 100.0F);
                    int j1 = k + 2;
                    this.renderAndDecorateCostA(p_283487_, itemstack1, itemstack, l, j1);
                    if (!itemstack2.isEmpty()) {
                        p_283487_.renderFakeItem(itemstack2, i + 5 + 35, j1);
                        p_283487_.renderItemDecorations(this.font, itemstack2, i + 5 + 35, j1);
                    }

                    this.renderButtonArrows(p_283487_, merchantoffer, i, j1);
                    p_283487_.renderFakeItem(itemstack3, i + 5 + 68, j1);
                    p_283487_.renderItemDecorations(this.font, itemstack3, i + 5 + 68, j1);
                    p_283487_.pose().popPose();
                    k += 20;
                    ++i1;
                } else {
                    ++i1;
                }


            }





            int k1 = this.shopItem;
            MerchantOffer merchantoffer1 = (MerchantOffer)merchantoffers.get(k1);
            if (((ModSettingMerchantMenu)this.menu).showProgressBar()) {
                this.renderProgressBar(p_283487_, i, j, merchantoffer1);
            }

            if (merchantoffer1.isOutOfStock() && this.isHovering(186, 35, 22, 21, (double)p_281994_, (double)p_282099_) && ((ModSettingMerchantMenu)this.menu).canRestock()) {
                p_283487_.renderTooltip(this.font, DEPRECATED_TOOLTIP, p_281994_, p_282099_);
            }

            for(TradeOfferButton ModMerchantScreen$tradeofferbutton : this.tradeOfferButtons) {
                if (ModMerchantScreen$tradeofferbutton.isHoveredOrFocused()) {
                    ModMerchantScreen$tradeofferbutton.renderToolTip(p_283487_, p_281994_, p_282099_);
                }

                ModMerchantScreen$tradeofferbutton.visible = ModMerchantScreen$tradeofferbutton.index < ((ModSettingMerchantMenu)this.menu).getOffers().size();
            }

            RenderSystem.enableDepthTest();
        }

        this.renderTooltip(p_283487_, p_281994_, p_282099_);
    }

    private void renderButtonArrows(GuiGraphics p_283020_, MerchantOffer p_281926_, int p_282752_, int p_282179_) {
        RenderSystem.enableBlend();
        if (p_281926_.isOutOfStock()) {
            p_283020_.blit(VILLAGER_LOCATION, p_282752_ + 5 + 35 + 20, p_282179_ + 3, 0, 25.0F, 171.0F, 10, 9, 512, 256);
        } else {
            p_283020_.blit(VILLAGER_LOCATION, p_282752_ + 5 + 35 + 20, p_282179_ + 3, 0, 15.0F, 171.0F, 10, 9, 512, 256);
        }

    }

    private void renderAndDecorateCostA(GuiGraphics p_281357_, ItemStack p_283466_, ItemStack p_282046_, int p_282403_, int p_283601_) {
        p_281357_.renderFakeItem(p_283466_, p_282403_, p_283601_);
        if (p_282046_.getCount() == p_283466_.getCount()) {
            p_281357_.renderItemDecorations(this.font, p_283466_, p_282403_, p_283601_);
        } else {
            p_281357_.renderItemDecorations(this.font, p_282046_, p_282403_, p_283601_, p_282046_.getCount() == 1 ? "1" : null);
            p_281357_.pose().pushPose();
            p_281357_.pose().translate(0.0F, 0.0F, 200.0F);
            String count = p_283466_.getCount() == 1 ? "1" : String.valueOf(p_283466_.getCount());
            this.font.drawInBatch(count, (float)(p_282403_ + 14) + 19.0F - 2.0F - (float)this.font.width(count), (float)p_283601_ + 6.0F + 3.0F, 16777215, true, p_281357_.pose().last().pose(), p_281357_.bufferSource(), DisplayMode.NORMAL, 0, 15728880, false);
            p_281357_.pose().popPose();
            p_281357_.pose().pushPose();
            p_281357_.pose().translate(0.0F, 0.0F, 300.0F);
            p_281357_.blit(VILLAGER_LOCATION, p_282403_ + 7, p_283601_ + 12, 0, 0.0F, 176.0F, 9, 2, 512, 256);
            p_281357_.pose().popPose();
        }

    }

    private boolean canScroll(int p_99141_) {
        return p_99141_ > 7;
    }

    public boolean mouseScrolled(double p_99127_, double p_99128_, double p_99129_) {
        int i = ((ModSettingMerchantMenu)this.menu).getOffers().size();
        if (this.canScroll(i)) {
            int j = i - 7;
            this.scrollOff = Mth.clamp((int)((double)this.scrollOff - p_99129_), 0, j);
        }

        return true;
    }

    public boolean mouseDragged(double p_99135_, double p_99136_, int p_99137_, double p_99138_, double p_99139_) {
        int i = ((ModSettingMerchantMenu)this.menu).getOffers().size();
        if (this.isDragging) {
            int j = this.topPos + 18;
            int k = j + 139;
            int l = i - 7;
            float f = ((float)p_99136_ - (float)j - 13.5F) / ((float)(k - j) - 27.0F);
            f = f * (float)l + 0.5F;
            this.scrollOff = Mth.clamp((int)f, 0, l);
            return true;
        } else {
            return super.mouseDragged(p_99135_, p_99136_, p_99137_, p_99138_, p_99139_);
        }
    }

    public boolean mouseClicked(double p_99131_, double p_99132_, int p_99133_) {

        this.isDragging = false;
        this.isFinished=false;
        int i = (this.width - this.imageWidth) / 2;
        int j = (this.height - this.imageHeight) / 2;
        if (this.canScroll(((ModSettingMerchantMenu)this.menu).getOffers().size()) && p_99131_ > (double)(i + 94) && p_99131_ < (double)(i + 94 + 6) && p_99132_ > (double)(j + 18) && p_99132_ <= (double)(j + 18 + 139 + 1)) {
            this.isDragging = true;
        }

        return super.mouseClicked(p_99131_, p_99132_, p_99133_);
    }

    @OnlyIn(Dist.CLIENT)
    class TradeOfferButton extends Button {
        final int index;

        public TradeOfferButton(int p_99205_, int p_99206_, int p_99207_, OnPress p_99208_) {
            super(p_99205_, p_99206_, 88, 20, CommonComponents.EMPTY, p_99208_, DEFAULT_NARRATION);
            this.index = p_99207_;
            this.visible = false;
        }

        public int getIndex() {
            return this.index;
        }

        public void renderToolTip(GuiGraphics p_281313_, int p_283342_, int p_283060_) {
            if (this.isHovered && ((ModSettingMerchantMenu) ModSettingMerchantScreen.this.menu).getOffers().size() > this.index + ModSettingMerchantScreen.this.scrollOff) {
                if (p_283342_ < this.getX() + 20) {
                    ItemStack itemstack = ((MerchantOffer)((ModSettingMerchantMenu) ModSettingMerchantScreen.this.menu).getOffers().get(this.index + ModSettingMerchantScreen.this.scrollOff)).getCostA();
                    p_281313_.renderTooltip(ModSettingMerchantScreen.this.font, itemstack, p_283342_, p_283060_);
                } else if (p_283342_ < this.getX() + 50 && p_283342_ > this.getX() + 30) {
                    ItemStack itemstack2 = ((MerchantOffer)((ModSettingMerchantMenu) ModSettingMerchantScreen.this.menu).getOffers().get(this.index + ModSettingMerchantScreen.this.scrollOff)).getCostB();
                    if (!itemstack2.isEmpty()) {
                        p_281313_.renderTooltip(ModSettingMerchantScreen.this.font, itemstack2, p_283342_, p_283060_);
                    }
                } else if (p_283342_ > this.getX() + 65) {
                    ItemStack itemstack1 = ((MerchantOffer)((ModSettingMerchantMenu) ModSettingMerchantScreen.this.menu).getOffers().get(this.index + ModSettingMerchantScreen.this.scrollOff)).getResult();
                    p_281313_.renderTooltip(ModSettingMerchantScreen.this.font, itemstack1, p_283342_, p_283060_);
                }
            }

        }

        @Override
        public void onPress() {
            selected=this.getIndex()+scrollOff;
            Minecraft.getInstance().player.sendSystemMessage(Component.literal(""+selected));
            super.onPress();
        }
        @Override
        public boolean mouseClicked(double mouseX, double mouseY, int button) {
//            return super.mouseClicked(mouseX, mouseY, button);
            if (this.active && this.visible) {
                if (this.isValidClickButton(button)) {
                    boolean flag = this.clicked(mouseX, mouseY);
                    if (flag) {
                        this.playDownSound(Minecraft.getInstance().getSoundManager());
                        isEditing=false;
                        onPress();
                        if(selected==this.getIndex()+scrollOff)//如果被选中了
                        {
//                            EditMode(selected);
                            return false;
                        }

                        return true;
                    }
                }

                return false;
            } else {
                return false;
            }
        }
    }


}